const canvas = document.getElementById('lotteryCanvas');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = 150;

let sectors = [];
let damages = [2, 1, 1, 0, 0]; // 5个扇形的伤害值
let isSpinning = false;
let spinTimeout;
let spinAngleStart = 0;
let spinTime = 0;
const spinTimeTotal = 3000; // 固定旋转时间为3秒

// 生成随机颜色且不重复
function generateRandomColors(count) {
    let colors = [];
    while (colors.length < count) {
        let color = `hsl(${Math.random() * 360}, 100%, 50%)`;
        if (!colors.includes(color)) {
            colors.push(color);
        }
    }
    return colors;
}

// 生成扇形
function generateSectors() {
    let totalAngle = 0;
    sectors = [];
    let colors = generateRandomColors(5); // 生成5个不重复的随机颜色

    for (let i = 0; i < 5; i++) {
        let maxAngle = Math.PI; // 最大角度为180°（π弧度）
        let remainingAngle = Math.PI * 2 - totalAngle;
        let angle = Math.random() * Math.min(maxAngle, remainingAngle - 0.35) + 0.35; // 最小20度（0.35弧度）
        sectors.push({ angle, color: colors[i] });
        totalAngle += angle;
    }
    // 最后一个扇形的角度补齐360度
    if (totalAngle < Math.PI * 2) {
        sectors.push({ angle: Math.PI * 2 - totalAngle, color: generateRandomColors(1)[0] });
    }
}

// 绘制扇形
function drawSectors() {
    let startAngle = 0;
    sectors.forEach((sector, index) => {
        let { angle, color } = sector;
        ctx.beginPath();
        ctx.moveTo(centerX, centerY);
        ctx.arc(centerX, centerY, radius, startAngle, startAngle + angle);
        ctx.closePath();
        ctx.fillStyle = color;
        ctx.fill();
        // 绘制黑色边框
        ctx.strokeStyle = '#000';
        ctx.lineWidth = 2;
        ctx.stroke();
        // 绘制伤害值
        ctx.fillStyle = '#000';
        ctx.font = '24px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        let midAngle = startAngle + angle / 2;
        let textX = centerX + (radius * 0.6) * Math.cos(midAngle);
        let textY = centerY + (radius * 0.6) * Math.sin(midAngle);
        ctx.fillText(damages[index] || 0, textX, textY);
        startAngle += angle;
    });
}

// 绘制指针
function drawPointer() {
    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.lineTo(centerX + radius * Math.cos(spinAngleStart), centerY + radius * Math.sin(spinAngleStart));
    ctx.strokeStyle = '#000';
    ctx.lineWidth = 2;
    ctx.stroke();
}

// 旋转动画
function spin() {
    spinTime += 30;
    if (spinTime >= spinTimeTotal) {
        stopSpin();
        return;
    }
    let spinAngle = spinAngleStart + easeOut(spinTime, 0, Math.PI * 6, spinTimeTotal);
    spinAngle %= Math.PI * 2;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawSectors();
    drawPointer();
    spinAngleStart = spinAngle;
    spinTimeout = setTimeout(spin, 30);
}

// 停止旋转
function stopSpin() {
    clearTimeout(spinTimeout);
    isSpinning = false;
    let finalAngle = spinAngleStart % (Math.PI * 2);
    let startAngle = 0;
    for (let i = 0; i < sectors.length; i++) {
        if (finalAngle >= startAngle && finalAngle < startAngle + sectors[i].angle) {
            alert(`你抽中了 ${damages[i] || 0} 点伤害！`);
            break;
        }
        startAngle += sectors[i].angle;
    }
}

// 缓动函数
function easeOut(t, b, c, d) {
    t /= d;
    return -c * t * (t - 2) + b;
}

// 初始化
function init() {
    generateSectors();
    drawSectors();
    drawPointer();
}

// 点击事件
canvas.addEventListener('click', () => {
    if (!isSpinning) {
        isSpinning = true;
        spinTime = 0;
        spin();
    } else {
        setTimeout(() => {
            stopSpin();
        }, 1000); // 1秒后停止
    }
});

init();